package com.isamarasinghe.srisinhala.controller;

import com.isamarasinghe.srisinhala.util.URLConnectionReader;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 
 * @author Isuru
 * 
 */
public class Convert extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html; charset=UTF-8");
		PrintWriter out = resp.getWriter();
		String source = req.getParameter("url");
		String output;

		if ((source == null) || (source.length() == 0)) {
			resp.sendRedirect("/index.html");
		} else {
			try {
				output = URLConnectionReader.read(source);
			} catch (Exception e) {
				output = "Invallied URL";
			}
			out.print(output);
			out.flush();
			out.close();
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		doPost(req, resp);
	}
}
